package cn.itsource.xhy.org.service.impl;

import cn.itsource.xhy.basic.service.impl.BaseServiceImpl;
import cn.itsource.xhy.org.domain.Employee;
import cn.itsource.xhy.org.domain.Shop;
import cn.itsource.xhy.org.domain.ShopLog;
import cn.itsource.xhy.org.mapper.EmployeeMapper;
import cn.itsource.xhy.org.mapper.ShopLogMapper;
import cn.itsource.xhy.org.mapper.ShopMapper;
import cn.itsource.xhy.org.service.IShopService;
import cn.itsource.xhy.user.domain.LoginInfo;
import cn.itsource.xhy.user.domain.User;
import cn.itsource.xhy.user.mapper.LoginInfoMapper;
import cn.itsource.xhy.utils.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.UUID;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ShopLogMapper shopLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public void settlement(Shop shop) {
        // 设置shop状态默认值
        shop.setState(0);
        // 设置默认时间默认值
        shop.setRegisterTime(new Date());
        // 设置employee状态默认值
        shop.getAdmin().setState(0);
        // 拿到emp对象
        Employee employee = shop.getAdmin();
        // 给用户密码生成盐值
        String salt = UUID.randomUUID().toString();
        // 工具类密码加密
        String str = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        // 保存加密后的密码
        employee.setPassword(str);
        // 保存盐值
        employee.setSalt(salt);
        // 保存shop中的admin对象
        employeeMapper.save(employee);
        // super调用父级保存对象
        super.save(shop);
        // 将将employee对象转换为LonginInfo对象
        LoginInfo loginInfo = empToLoginInfo(employee);
        // 保存
        loginInfoMapper.save(loginInfo);

    }

    // 将employee对象转换为LonginInfo对象
    private LoginInfo empToLoginInfo(Employee employee){
        // loginInfo对象
        LoginInfo loginInfo = new LoginInfo();
        // gjl复制数据
        BeanUtils.copyProperties(employee,loginInfo);
        // 其他数据赋值
        loginInfo.setDisable(1);
        loginInfo.setType(0);
        loginInfo.setUId(employee.getId());
        return loginInfo;
    }

    @Override
    public void setShop(Shop shop) {
        System.out.println(shop);
        // 设置shop状态默认值
        shop.setState(0);
        // super调用父级保存对象
        super.update(shop);
    }

    @Override
    public Shop loadByAdminId(Long id) {
        return shopMapper.loadByAdminId(id);
    }

    @Override
    public void reject(ShopLog shopLog) throws MessagingException {
        // 将门店状态设置为驳回状态-1
        Shop shop = shopMapper.findById(shopLog.getShop_id());
        // 状态设置为-1
        shop.setState(-1);
        // 修改
        shopMapper.update(shop);
        System.out.println("========="+shop);
        // 记录日志
        Employee employee = new Employee();
        employee.setId(1L);
        shopLog.setEmployee(employee);
        shopLogMapper.save(shopLog);
        System.out.println(shopLog);
        // 驳回后发送邮件
        // 创建一个复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 使用工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        // 设置邮件来源
        helper.setFrom("1215985121@qq.com");
        // 设置邮件主题
        helper.setSubject("审核驳回");
        // 设置邮件内容
        helper.setText("<h1>审核失败</h1><br>"
            +"<span>失败原因："+shopLog.getNote()+"</span>"
            +"<br><a href='http://127.0.0.1:8081/#/register2?id="+shop.getId()+"'>重新提交审核</a>",true);
        // 设置附件
        // helper.addAttachment("点我试试.jpg",new File("C:\\Users\\86152\\Desktop\\图片\\4号技师.jpg"));
        // 设置发送给谁
        Employee emp = employeeMapper.findById(shop.getAdmin_id());
        String email = emp.getEmail();
        helper.setTo(email);
        // 发送信息
        javaMailSender.send(mimeMessage);

    }

    @Override
    public void pass(ShopLog shopLog) throws MessagingException {
        // 通过店铺id查询到店铺
        Shop shop = shopMapper.findById(shopLog.getShop_id());
        // 将店铺状态设置为1
        shop.setState(1);
        // 修改shop
        shopMapper.update(shop);
        // 记录日志
        Employee employee = new Employee();
        employee.setId(1L);
        shopLog.setEmployee(employee);
        shopLogMapper.save(shopLog);
        // 审核通过后发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        // 发送邮件的来源
        helper.setFrom("1215985121@qq.com");
        // 邮件标题
        helper.setSubject("审核通过");
        // 负责人id
        Long id = shop.getAdmin_id();
        // 邮件内容
        helper.setText("<a href='http://127.0.0.1:8080/emp/pass/"+id+"'>立即激活</a>",true);
        Employee emp = employeeMapper.findById(id);
        String email = emp.getEmail();
        // 发送邮件
        helper.setTo(email);
        // 发送信息
        javaMailSender.send(mimeMessage);
    }
}
